<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../../assets/img/headerLogo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="danger" @click="logout">注销</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path + ''"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
       <router-view></router-view>
        </el-main>
    </el-container>
  </el-container>
</template>

<script>
import Welcome from './childComponents/Welcome'

export default {
  created() {
    this.getMenuList(),
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  name: 'Home',
  data() {
    return {
      //左侧菜单数据
      menuList: [],
      iconsList: {
        125: 'el-icon-user',
        103: 'el-icon-unlock',
        101: 'el-icon-shopping-bag-1',
        102: 'el-icon-tickets',
        145: 'el-icon-monitor',
      },
      isCollapse: false,
      activePath:''
    }
  },
  methods: {
    //注销功能
    logout() {
      window.sessionStorage.clear()
      this.$router.push('./login')
      this.$message.success('注销成功')
    },
    //左侧菜单栏折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    //菜单高亮
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    },
    //获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
  },
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #fff;
  border-bottom: 2px solid rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  img {
    height: 55px;
  }
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #fff;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.toggle-button {
  text-align: center;
  height: 25px;
  line-height: 25px;
  border-bottom: 2px solid rgba(0, 0, 0, 0.1);
  letter-spacing: 0.2em;
  font-size: 10px;
  cursor: pointer;
}
</style>